/**
 * BBD Service Inc
 * All Rights Reserved @2018
 */
package me.gavincook.moon.plugin.util;

/**
 * 字符串工具类
 *
 * @author gavincook
 * @version $ID: StringUtils.java, v0.1 2018-05-02 23:32 gavincook Exp $$
 */
public class StringUtils {

    /**
     * 获取多个字符串最长的公共字符串前缀
     * @param strings
     * @return
     */
    public static String getLongestCommonPrefix(String[] strings) {
        if (strings == null || strings.length == 0) {
            return null;
        }
        if (strings.length == 1) {
            return strings[0];
        }

        int currentCharIndex = 0;
        for (; currentCharIndex < strings[0].length(); currentCharIndex++) {
            for (int currentStrIndex = 1; currentStrIndex < strings.length; currentStrIndex++) {
                //如果到达字符串末尾或者找到不相等的字符串，则返回匹配
                if (currentCharIndex > strings[currentStrIndex].length()
                    || strings[0].charAt(currentCharIndex) != strings[currentStrIndex].charAt(currentCharIndex)) {
                    return strings[0].substring(0, currentCharIndex);
                }
            }
        }

        return strings[0].substring(0, currentCharIndex);
    }

}
